﻿// ECMAScript 5 strict mode
"use strict";

assert2(cr, "cr namespace not created");
assert2(cr.plugins_, "cr.plugins_ not created");


goog.provide("galactica.c2interface");

goog.require("galactica.controller");
goog.require("galactica.deck");


/////////////////////////////////////
// Plugin class
// *** CHANGE THE PLUGIN ID HERE *** - must match the "id" property in edittime.js
//          vvvvvvvv
cr.plugins_.GalacticaCIC = function(runtime)
{
	this.runtime = runtime;
};

(function ()
{
    
    var galacticaController = galactica.controller();
    
    
	/////////////////////////////////////
	// *** CHANGE THE PLUGIN ID HERE *** - must match the "id" property in edittime.js
	//                            vvvvvvvv
	var pluginProto = cr.plugins_.GalacticaCIC.prototype;
		
	/////////////////////////////////////
	// Object type class
	pluginProto.Type = function(plugin)
	{
		this.plugin = plugin;
		this.runtime = plugin.runtime;
	};

	var typeProto = pluginProto.Type.prototype;

	// called on startup for each object type
	typeProto.onCreate = function()
	{
	};

	/////////////////////////////////////
	// Instance class
	pluginProto.Instance = function(type)
	{
		this.type = type;
		this.runtime = type.runtime;
		
		// any other properties you need, e.g...
		// this.myValue = 0;
	};
	
	var instanceProto = pluginProto.Instance.prototype;

	// called whenever an instance is created
	instanceProto.onCreate = function()
	{
		// note the object is sealed after this call; ensure any properties you'll ever need are set on the object
		// e.g...
		// this.myValue = 0;
	};
	
	// called whenever an instance is destroyed
	// note the runtime may keep the object after this call for recycling; be sure
	// to release/recycle/reset any references to other objects in this function.
	instanceProto.onDestroy = function ()
	{
	};
	
	// called when saving the full state of the game
	instanceProto.saveToJSON = function ()
	{
		// return a Javascript object containing information about your object's state
		// note you MUST use double-quote syntax (e.g. "property": value) to prevent
		// Closure Compiler renaming and breaking the save format
		return {
			// e.g.
			//"myValue": this.myValue
		};
	};
	
	// called when loading the full state of the game
	instanceProto.loadFromJSON = function (o)
	{
		// load from the state previously saved by saveToJSON
		// 'o' provides the same object that you saved, e.g.
		// this.myValue = o["myValue"];
		// note you MUST use double-quote syntax (e.g. o["property"]) to prevent
		// Closure Compiler renaming and breaking the save format
	};
	
	// only called if a layout object - draw to a canvas 2D context
	instanceProto.draw = function(ctx)
	{
	};
	
	// only called if a layout object in WebGL mode - draw to the WebGL context
	// 'glw' is not a WebGL context, it's a wrapper - you can find its methods in GLWrap.js in the install
	// directory or just copy what other plugins do.
	instanceProto.drawGL = function (glw)
	{
	};

	//////////////////////////////////////
	// Conditions
	function Cnds() {};


	Cnds.prototype.CheckExpansionActivity = function (expansionId)
	{
		switch(expansionId) {
		    case 0: {
		        return galacticaController.isExpansionPegasus();
		        break;
		    }
		    case 1: {
		        return galacticaController.isExpansionExodus();
		        break;
		    }
		    default: {
		        return false;
		    }
		}
	};
	
	Cnds.prototype.CheckRuleActivity = function(ruleId) {
	    switch(ruleId) {
	        case 0: {
	            return galacticaController.allowCylonLeaders();
	            break;
	        }
	        case 1: {
	            return galacticaController.canRefuseCylonLeader();
	            break;
	        }
	        case 2: {
	            return galacticaController.isExodusExtraLoyaltyCardAdded();
	            break;
	        }
	        default: {
	            return false;
	            break;
	        }
	    }
	};
	
	// ... other conditions here ...
	
	pluginProto.cnds = new Cnds();
	
	//////////////////////////////////////
	// Actions
	function Acts() {};

	// the example action
	Acts.prototype.AddExpansion = function (expansionId, toggle)
	{
		switch(expansionId) {
            case 0: {
                galacticaController.setExpansionPegasus(toggle);
                break;
            }
            case 1: {
                return galacticaController.setExpansionPegasus(toggle);
                break;
            }
            default: {
                break;
            }
        }
	};
	
	Acts.prototype.CreateCharacterDeck = function() {
	    galacticaController.setDeck();
	};
	
	
	pluginProto.acts = new Acts();
	
	//////////////////////////////////////
	// Expressions
	function Exps() {};
	
	// the example expression
	Exps.prototype.MyExpression = function (ret)	// 'ret' must always be the first parameter - always return the expression's result through it!
	{
		ret.set_int(1337);				// return our value
		// ret.set_float(0.5);			// for returning floats
		// ret.set_string("Hello");		// for ef_return_string
		// ret.set_any("woo");			// for ef_return_any, accepts either a number or string
	};
	
	
	Exps.prototype.GetCharacterAmount = function(ret) {
	    var deck = galacticaController.getDeck();
	    ret.set_int(deck.deckSize() );
	};
	
	Exps.prototype.GetCharacterInfo = function(ret, characterIndex, infoType) {
	   var deck = galacticaController.getDeck();
	   //console.log("Get card with id "+characterIndex+" for infotype "+infoType);
	   var card = deck.getCard(characterIndex);
	   console.log(JSON.stringify(card));
	   switch(infoType) {
	       case "name": {
	           ret.set_string(card.getName());
	           break;
	       }
	       case "type": {
	           var type = card.getType().replace(/\s/g, "");
	           ret.set_string(type);
	           break;
	       }
	       case "admiral inheritance": {
	           ret.set_string(card.getAdmiralInheritance().toString());
	           break;
	       }
	       case "cag inheritance": {
	           ret.set_string(card.getCAGInheritance().toString());
	           break;
	       }
	       case "president inheritance": {
	           ret.set_string(card.getPresidentInheritance().toString());
	           break;
	       }
	       case "set": {
	           ret.set_string(card.getSet());
	           break;
	       }
	       case "start location": {
	           ret.set_string(card.getStartingLocation());
	           break;
	       }
	       case "oncePerTurnTitle" : {
	           var ability = card.getOncePerTurnAbility();
	           ret.set_string(ability[0]);
	           break;
	       }
	       case "oncePerTurnText" : {
	           var ability = card.getOncePerTurnAbility();
	           ret.set_string(ability[1]);
	           break;
	       }
	       case "oncePerGameTitle": {
	           var ability = card.getOncePerGameAbility();
	           ret.set_string(ability[0]);
	           break;
	       }
	       case "oncePerGameText" : {
	           var ability = card.getOncePerGameAbility();
	           ret.set_string(ability[1]);
	           break;
	       }
	       case "weaknessTitle" : {
	           var ability = card.getWeakness();
	           ret.set_string(ability[0]);
	           break;
	       }
	       case "weaknessText" : {
	           var ability = card.getWeakness();
	           ret.set_string(ability[1]);
	           break;
	       }
	       case "loyaltyWeight": {
	           ret.set_string(card.getLoyaltyWeight().toString());
	           break;
	       }
	       default: {
	           ret.set_string("Error");
	           break;
	       }
	   }//Switch  
	};
	
	
	pluginProto.exps = new Exps();

}());